package com.hkw.service.impl;

import com.hkw.dao.IStudentDao;
import com.hkw.factory.StudentDaoFactory;
import com.hkw.model.Student;
import com.hkw.service.IStudentService;
import com.hkw.utils.PagesUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * Student服务接口实现类
 */
public class StudentServiceImpl implements IStudentService {
    private IStudentDao studentDao;
    private PagesUtils pagesUtils;
    //无参构造里利用工厂类生产对象给成员变量
    public StudentServiceImpl() {
        this.studentDao = StudentDaoFactory.getStudentDao();
    }
    //无参构造里利用工厂类生产对象给成员变量，分页工具类给成员变量
    public StudentServiceImpl(int nowpage) {
        this.studentDao = StudentDaoFactory.getStudentDao();
        try {
            this.pagesUtils = new PagesUtils(studentDao.findAllRows(),nowpage);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public PagesUtils getPagesUtils() {
        return pagesUtils;
    }

    public void setPagesUtils(PagesUtils pagesUtils) {
        this.pagesUtils = pagesUtils;
    }

    /**
     * 返回所有学生的列表
     * @return
     */
    @Override
    public List<Student> list() {
        try {
            return studentDao.findAll(pagesUtils);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @Override
    public int add(Student student) {
        try {
            return studentDao.insertStudent(student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    /**
     * 删除学生
     * @param id
     * @return
     */
    @Override
    public int del(int id) {
        try {
            return studentDao.deleteStudentById(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    /**
     * 寻找学生根据id
     * @param id
     * @return
     */
    @Override
    public Student findStudentById(int id) {
        try {
            return studentDao.findStudentById(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /**
     * 跟新学生信息
     * @param student
     * @return
     */
    @Override
    public int updateStudent(Student student) {
        try {
            return studentDao.updateStudent(student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    /**
     * 根据name和email寻找学生
     * @param name
     * @param email
     * @return
     */
    @Override
    public List<Student> findStudentByNameAndEmail(String name, String email,Integer classId) {
        try {
            return studentDao.findStudentByNameAndEmail(name,email,classId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
